﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Util;
using Emgu.CV.Features2D;
using Emgu.CV.Structure;
using Emgu.Util;

namespace VehicleTest
{
    public partial class DetectionRef : Form
    {
        private VehicleDetection detection;

        public DetectionRef(VehicleDetection vd)
        {
            InitializeComponent();
            detection = vd;

            radioButton_CORREL.Enabled = false;
            radioButton_BHATTACHARYYA.Enabled = false;
            radioButton_EMD.Enabled = false;

            numericUpDown_Hmax.Value = detection.Hmax_target;
            numericUpDown_Hmin.Value = detection.Hmin_target;
            numericUpDown_Smax.Value = detection.Smax_target;
            numericUpDown_Smin.Value = detection.Smin_target;
            numericUpDown_Vmax.Value = detection.Vmax_target;
            numericUpDown_Vmin.Value = detection.Vmin_target;

            numericUpDown_Threshold.Value = (decimal)detection.mask_threshold;
            numericUpDown_DilateTimes.Value = detection.dilate_times;
            numericUpDown_ErodeTimes.Value = detection.erode_times;
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            e.Cancel = true;   //此时单击关闭按钮后并不释放资源,此窗口的资源在VehicleDetection窗口中释放
            detection.toolStripButton_Ref.Text = "打开参考";
            detection.ref_open = false;
            this.Hide();

            //还原初始状态
            detection.hist_target_show = false;
            button_ShowHistogram.Text = "显示直方图";

            detection.data_show = false;
            button_ShowData.Text = "显示数据";

            checkBox_ShowData1.Checked = false;
            checkBox_ShowData2.Checked = false;
            detection.histdata_target_show = false;
            detection.coeffdata_show = false;

            radioButton_CORREL.Enabled = false;
            radioButton_BHATTACHARYYA.Enabled = false;
            radioButton_EMD.Enabled = false;

            checkBox_CancelErode.Checked = false;
            detection.erode_cancel = false;
            checkBox_CancelDilate.Checked = false;
            detection.dilate_cancel = false;

            detection.mask_show = false;
            button_ShowMask.Text = "显示掩码";

            detection.mask_cancel = false; //使用掩码

            base.OnFormClosing(e);   //在MSDN中规定如果重载了OnFormClosing函数，最后要调用基类的OnFormClosing函数
        }

        private void button_clear_Click(object sender, EventArgs e)
        {
            richTextBox_Show.Clear();
        }

        private void button_ShowHistogram_Click(object sender, EventArgs e)
        {
            if (button_ShowHistogram.Text == "显示直方图")
            {
                detection.hist_target_show = true;
                button_ShowHistogram.Text = "停止显示";
            }
            else
            {
                detection.hist_target_show = false;
                button_ShowHistogram.Text = "显示直方图";
            }
            
        }

        private void button_ShowData_Click(object sender, EventArgs e)
        {
            if (button_ShowData.Text == "显示数据")
            {
                detection.data_show = true;
                button_ShowData.Text = "停止显示";
            }
            else
            {
                detection.data_show = false;
                button_ShowData.Text = "显示数据";
            }
        }

        private void checkBox_Data1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_ShowData1.Checked)
                detection.histdata_target_show = true;
            else
                detection.histdata_target_show = false;
        }

        private void checkBox_ShowData2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_ShowData2.Checked)
            {
                detection.coeffdata_show = true;
                radioButton_CORREL.Enabled = true;
                radioButton_BHATTACHARYYA.Enabled = true;
                radioButton_EMD.Enabled = true;
            }
            else
            {
                detection.coeffdata_show = false;
                radioButton_CORREL.Enabled = false;
                radioButton_BHATTACHARYYA.Enabled = false;
                radioButton_EMD.Enabled = false;
            }
        }

        private void numericUpDown_Hmin_ValueChanged(object sender, EventArgs e)
        {
            detection.Hmin_target = (int)Math.Round(numericUpDown_Hmin.Value);
        }

        private void numericUpDown_Hmax_ValueChanged(object sender, EventArgs e)
        {
            detection.Hmax_target = (int)Math.Round(numericUpDown_Hmax.Value);
        }

        private void numericUpDown_Smin_ValueChanged(object sender, EventArgs e)
        {
            detection.Smin_target = (int)Math.Round(numericUpDown_Smin.Value);
        }

        private void numericUpDown_Smax_ValueChanged(object sender, EventArgs e)
        {
            detection.Smax_target = (int)Math.Round(numericUpDown_Smax.Value);
        }

        private void numericUpDown_Vmin_ValueChanged(object sender, EventArgs e)
        {
            detection.Vmin_target = (int)Math.Round(numericUpDown_Vmin.Value);
        }

        private void numericUpDown_Vmax_ValueChanged(object sender, EventArgs e)
        {
            detection.Vmax_target = (int)Math.Round(numericUpDown_Vmax.Value);
        }

        private void radioButton_CORREL_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton_CORREL.Checked)
                detection.comp_method = HISTOGRAM_COMP_METHOD.CV_COMP_CORREL;
        }

        private void radioButton_BHATTACHARYYA_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton_BHATTACHARYYA.Checked)
                detection.comp_method = HISTOGRAM_COMP_METHOD.CV_COMP_BHATTACHARYYA;
        }

        private void radioButton_EMD_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton_EMD.Checked)
                detection.flag_EMD = true;
            else
                detection.flag_EMD = false;
        }

        private void numericUpDown_Threshold_ValueChanged(object sender, EventArgs e)
        {
            detection.mask_threshold = (double)Math.Round(numericUpDown_Threshold.Value);
        }


        private void numericUpDown_ErodeTimes_ValueChanged(object sender, EventArgs e)
        {
            detection.erode_times = (int)Math.Round(numericUpDown_ErodeTimes.Value);
        }

        private void checkBox_CancelErode_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_CancelErode.Checked)
                detection.erode_cancel = true;
            else
                detection.erode_cancel = false;
        }

        private void numericUpDown_DilateTimes_ValueChanged(object sender, EventArgs e)
        {
            detection.dilate_times = (int)Math.Round(numericUpDown_DilateTimes.Value);
        }

        private void checkBox_CancelDilate_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_CancelDilate.Checked)
                detection.dilate_cancel = true;
            else
                detection.dilate_cancel = false;
        }

        private void button_ShowMask_Click(object sender, EventArgs e)
        {
            if (button_ShowMask.Text == "显示掩码")
            {
                detection.mask_show = true;
                button_ShowMask.Text = "停止显示";
            }
            else
            {
                detection.mask_show = false;
                button_ShowMask.Text = "显示掩码";
            }

        }

        private void checkBox_CancelMask_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_CancelMask.Checked)
                detection.mask_cancel = true;
            else
                detection.mask_cancel = false;
        }

    }
}
